<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>跟随指针</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
        var canvas = document.getElementById('canvas')
        var ctx = canvas.getContext('2d')
        var ww = window.innerWidth,
            wh = window.innerHeight,
            ox = ww / 2,
            oy = wh / 2
        canvas.width = ww
        canvas.height = wh
        ctx.translate(ox, oy)
        var r = 0;
        function draw() {
            ctx.save();
            ctx.clearRect(-ww / 2, -wh / 2, ww, wh)
            ctx.rotate(r);
            line([0, 0], [100, 0], 8, '#25b863')
            line([0, 0], [-60, 0], 8, '#000')
            ctx.restore()
        }
        draw();
        function line(s, e, w, c) {
            ctx.beginPath();
            ctx.strokeStyle = c;
            ctx.lineWidth = w;
            ctx.moveTo(s[0], s[1])
            ctx.lineTo(e[0], e[1])
            ctx.stroke()
            ctx.closePath()
        }
        window.addEventListener('mousemove', (e) => {
            console.log(e)
            r = Math.atan2(e.clientY - oy, e.clientX - ox)
            draw()
        })
    </script>
</body>

</html>